package studio.wetrack.accountService.auth.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chen on 2018/5/25.
 *
 * 角色中的 权限集合在数据库里是以拼接字符串的形式存在
 * VIEW_MISSION*读任务#EDIT_MISSION*写任务
 */
public class UserType implements Serializable {

    long id;
    String name;  //角色名称
    List<RoleClass> roles;  //权限集合
    Boolean deleted;
    Boolean baseType;  //是否是基础预制角色

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<RoleClass> getRoles() {
        return roles;
    }

    public void setRoles(List<RoleClass> roles) {
        this.roles = roles;
    }

    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    public Boolean getBaseType() {
        return baseType;
    }

    public void setBaseType(Boolean baseType) {
        this.baseType = baseType;
    }

    public String toRoleString(){
        String roleString = "";
        if(this.roles == null || 0 == this.roles.size()){
            return roleString;
        }
        for(int i=0;i<this.roles.size();i++){
            if(0 == i){
                roleString = roleString + this.roles.get(i).getRoleName() + "*" + this.roles.get(i).getDesc();
            }else{
                roleString = roleString + "#" + this.roles.get(i).getRoleName() + "*" + this.roles.get(i).getDesc();
            }
        }
        return roleString;
    }

    public List<RoleClass> toArray(String roleString){
        List<RoleClass> list = new ArrayList<RoleClass>();
        if(null == roleString || roleString.length() == 0){
            return list;
        }
        String[] roles = roleString.split("#");
        for(int i=0;i<roles.length;i++){
            RoleClass roleClass = new RoleClass();
            String[] a = roles[i].split("\\*");
            roleClass.setRoleName(a[0]);
            roleClass.setDesc(a[1]);
            list.add(roleClass);
        }
        return list;
    }
}
